package com.heo.coder.packet;

import com.heo.coder.packet.base.BasePacket;
import com.heo.util.ByteHelper;
import com.heo.util.Utils;

public class StartPacketRsp extends BasePacket{

	public StartPacketRsp(String head, String id, int bodyLength, String msta,
			String control, String body, String check) throws Exception {
		super(head, id, bodyLength, msta, control, body, check);
	}
	
	/**
	 * 
	 * @param passageway  枪号
	 * @param userMobile  用户账号
	 * @param sett  结算方式
	 * @param voltage  电压类型 
	 * @param chargePower 充电电量
	 * @param chargeAmount 充电金额 
	 */
	public String setMsgContent(int passageway, String userMobile , int sett, int voltage, float chargePower, float chargeAmount, float fee) {
		setBodyLengthStr(ByteHelper.strReversal(ByteHelper.intToHexString(63, 4)));
		StringBuilder sBuilder = new StringBuilder();
		String passStr = ByteHelper.intToHexString(passageway, 2);
		userMobile = "0" + userMobile;
		String userMobileStr = ByteHelper.stringToAscii(userMobile);
		String settStr = ByteHelper.intToHexString(sett, 2);
		String voltageStr = ByteHelper.intToHexString(voltage, 2);
		String chargePowerStr = ByteHelper.strReversal(ByteHelper.floatToHexString(chargePower, 8));
		String chargeAmountStr = ByteHelper.strReversal(ByteHelper.floatToHexString(chargeAmount, 8));
		
		String batch = Utils.timeStamp();
		String timeCode = ByteHelper.intToHexString(Utils.getInteger(batch), 8);
		timeCode = ByteHelper.strReversal(timeCode.toUpperCase());
		
		String checkCode = ByteHelper.stringToAscii(batch.substring(batch.length() - 4, batch.length()));
		System.out.println(checkCode);
		sBuilder.append(passStr).append(userMobileStr).append(settStr).append(voltageStr).append(chargePowerStr).append(chargeAmountStr);
		sBuilder.append(timeCode).append(checkCode);
		
		String fee1 = ByteHelper.strReversal(ByteHelper.floatToHexString(fee, 4));
		String fee2 = ByteHelper.strReversal(ByteHelper.floatToHexString(fee, 4));
		String fee3 = ByteHelper.strReversal(ByteHelper.floatToHexString(fee, 4));
		String fee4 = ByteHelper.strReversal(ByteHelper.floatToHexString(fee, 4));
		sBuilder.append(fee1).append(fee2).append(fee3).append(fee4);
		
		sBuilder.append(ByteHelper.intToHexString(1, 2))
		.append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2));
		sBuilder.append(ByteHelper.intToHexString(1, 2))
		.append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2));
		sBuilder.append(ByteHelper.intToHexString(1, 2))
		.append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2));
		sBuilder.append(ByteHelper.intToHexString(1, 2))
		.append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2)).append(ByteHelper.intToHexString(1, 2));
		
		setBody(sBuilder.toString());
		String check = ByteHelper.makeCheckSum(START_MARKER_LENGTH_CODE + getHead() + getMsta() + getId() + START_MARKER_LENGTH_CODE + getControl() + getBodyLengthStr() + getBody());
		setCheck(check);
		return batch;
	}
	
	@Override
	public String toString() {
		return "SocketPacket: [" + control + "] [" + START_MARKER_LENGTH_CODE + getHead() + getMsta() + getId() + START_MARKER_LENGTH_CODE + getControl() +  getBodyLengthStr() + getBody() + getCheck() + END_MARKER_LENGTH_CODE + "]";
	}

	@Override
	public String getPacketBody() {
		return super.body;
	}

	
	
}
